using System;
using System.Collections.Generic;
using System.Text;
using BizTalkCop.Rules.Bases;
using Microsoft.FxCop.Sdk;

namespace BizTalkCop.Rules.Naming.Rules
{
    /// <summary>
    /// Rule verifying pipeline names are correctly suffixed "Send" or "Receive"
    /// </summary>
    public class PipelineNamesShouldIndicateDirection : ConfiguredRuleBase
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public PipelineNamesShouldIndicateDirection() : base("PipelineNamesShouldIndicateDirection") { }

        /// <summary>
        /// Checks if the pipeline name is correctly suffixed
        /// </summary>
        /// <param name="type">Type to check</param>
        /// <returns>Problem collection</returns>
        public override ProblemCollection Check(TypeNode type)
        {
            string receiveSuffix = this.GetConfiguredSetting(Setting.ReceiveSuffix);
            string sendSuffix = this.GetConfiguredSetting(Setting.SendSuffix);
            if (IsReceivePipeline(type) && !type.Name.Name.EndsWith(receiveSuffix))
            {
                Problems.Add(new Problem(GetNamedResolution("Suffix", receiveSuffix), type));
            }
            else if (IsSendPipeline(type) && !type.Name.Name.EndsWith(sendSuffix))
            {
                Problems.Add(new Problem(GetNamedResolution("Suffix", sendSuffix), type));
            }
            return Problems;
        }

        #region Configured settings

        static PipelineNamesShouldIndicateDirection()
        {
            Dictionary<string, string> defaults = new Dictionary<string, string>();
            defaults.Add(Setting.ReceiveSuffix, Default.ReceiveSuffix);
            defaults.Add(Setting.SendSuffix, Default.SendSuffix);
            SetDefaults(typeof(PipelineNamesShouldIndicateDirection), defaults);
        }

        private struct Setting
        {
            public const string ReceiveSuffix = "ReceiveSuffix";
            public const string SendSuffix = "SendSuffix";
        }

        private struct Default
        {
            public const string ReceiveSuffix = "Receive";
            public const string SendSuffix = "Send";
        }

        #endregion
    }
}
